#pragma once
#include "Connection.hpp"
#include <iostream>
#include "Common.hpp"
#include "Log.hpp"
#include<memory>
#include<functional>
using namespace LogMoudle;
const int SIZE=1024;
class Channel : public Connection,
public std::enable_shared_from_this<Channel>
{
private:
    std::string inbuffer;
    std::string outbuffer;
    NetAddr client_addr;
    uint32_t events;
    int sockfd;
public:
    Channel(int sockfd)
    {
        SetNonBlock(sockfd);
        setfd(sockfd);
    }
    ~Channel() {}
    //保证tcp本轮数据读完
    //保证报文完整性
    //粘包问题
    //协议问题
    void Recever() 
    {
        char buffer[SIZE];
        while(true){
            buffer[0]='\0';
            ssize_t n = recv(sockfd, buffer, SIZE, 0);
            LOG(LogLevel::DEBUG)<<"recv:"<<buffer;
            LOG(LogLevel::DEBUG)<<"n:"<<n;
            if(n>0){
               buffer[n]=0;
               inbuffer+=buffer;
            }else if(n==0){
                //异常处理
                LOG(LogLevel::DEBUG)<<"客户端关闭";
                Excepter();
                break;
            }else{
                if(errno==EAGAIN||errno==EWOULDBLOCK){
                    //本轮数据读完
                    LOG(LogLevel::DEBUG)<<"本轮数据读完";
                    break;
                }else if(errno==EINTR){
                    //信号中断
                    LOG(LogLevel::DEBUG)<<"EINTR";
                    continue;
                }else{
                    //错误处理
                    LOG(LogLevel::ERROR)<<"error";
                    Excepter();
                    break;
                }
            }
        }
        //处理粘包
        //处理数据，反序列化
        if(!inbuffer.empty()){
            //handler(std::shared_ptr<Channel>(this));
            handler(shared_from_this());
            if(!outbuffer.empty()){
               Sender();
            }
        }
    }
    std::string& Inbuffer(){
        return inbuffer;
    }
    void Sender() 
    {
        while(true){
            ssize_t n = send(sockfd, outbuffer.c_str(), outbuffer.size(), 0);
            LOG(LogLevel::DEBUG)<<"send:"<<outbuffer.c_str();
            LOG(LogLevel::DEBUG)<<"n:"<<n;
            if(n>0){
                outbuffer.erase(0,n);
            }else if(n==0){
                //空字符
                break;
            }else{
                if(errno==EAGAIN||errno==EWOULDBLOCK){
                    //缓冲区被写满
                    LOG(LogLevel::DEBUG)<<"缓冲区被写满";
                    break;
                }else if(errno==EINTR){
                    //信号中断
                    std::cout<<"EINTR"<<std::endl;
                    continue;
                }else if(errno==EPIPE){
                    LOG(LogLevel::DEBUG)<<"客户端关闭";
                    Excepter();
                    break;
                }
                else{
                    //错误处理
                    LOG(LogLevel::ERROR)<<"error";
                    Excepter();
                    break;
                }
            }
        }
        //1.数据发送完毕
        //2.发送条件不具备
        if(!outbuffer.empty()){
            //开启对写事件的关心
            getOwner()->EnableReadWrite(sockfd,true,true);
        }else{
            getOwner()->EnableReadWrite(sockfd,true,false);
        }
    }
    std::string AppendOutbuffer(const std::string& out){
        outbuffer+=out;
        LOG(LogLevel::DEBUG)<<"AppendOutbuffer:"<<out;
        return outbuffer;
    }
    void Excepter() 
    {
        getOwner()->Deleteconnection(sockfd);
    }
    int getFd() override{return sockfd;}
    void setfd(int sockfd) {
        this->sockfd = sockfd;
    }
};